<?php
class Bliss_View_Port
{
	/**
	 * View Object
	 * @var Zend_View
	 */
	protected $_view;

	/**
	 * REQUEST Params
	 * @var Zend_Config
	 */
	protected $_params;

	/**
	 * Page sections
	 * @var array
	 */
	protected $_sections;

	/**
	 * Constructor
	 *
	 * @param Zend_Controller_Action $controller
	 * @param array $sections
	 */
	public function __construct(Zend_View $view, array $sections = null)
	{
		$this->_view	= $view;
		$this->setParams($_REQUEST);
		$this->setSections($sections);
	}

	/**
	 * Sets the parameters
	 *
	 * @param array $params
	 */
	public function setParams($params)
	{
		$this->_params	= new Zend_Config($params);
	}

	/**
	 * Sets the sections
	 *
	 * @param array $section
	 * @return $this
	 */
	public function setSections(array $sections = null)
	{
		$this->clearSections()->addSections($sections);
		return $this;
	}

	/**
	 * Adds a section
	 *
	 * @param string $name
	 * @param mixed $data
	 * @return $this
	 */
	public function addSection($name, $data)
	{
		$this->_sections[$name]	= $data;
		return $this;
	}

	/**
	 * Adds many sections
	 * 
	 * @param array $sections
	 * @return $this
	 */
	public function addSections(array $sections = null)
	{
		if(isset($sections)) {
			foreach($sections as $name => $data) {
				$this->addSection($name, $data);
			}
		}
		return $this;
	}

	/**
	 * Clears the sections
	 * @return $this
	 */
	public function clearSections()
	{
		unset($this->_sections);
		return $this;
	}

	/**
	 * Processes the view
	 *
	 * @param array $sections
	 * @param array $params
	 */
	public function process(array $sections = null, array $params = null)
	{
		$format	= $this->_params->format;
		if($format == 'json') {
			$sectionName	= $this->_params->section;
			$section		= $this->getSection($sectionName, true);
			$data			= isset($section) ? $section : $this->getSections(true);

			header("Content-Type: application/json");
			echo $this->_view->json($data);
			exit;
		} elseif($format == 'ajax') {
			$this->_view->pageHeader = false;
			$this->_view->layout()->disableLayout();
		} else {
			$sections	= $this->getSections();
			foreach($sections as $name => $data) {
				$this->_view->assign($name, $data);
			}
		}
	}

	/**
	 * Gets a single section
	 *
	 * @param string $sectionName
	 * @param bool $convert
	 * @return mixed
	 */
	public function getSection($sectionName, $convert = false)
	{
		if(isset($this->_sections) && isset($this->_sections[$sectionName])) {
			$section	= $this->_sections[$sectionName];
			if($convert !== false && is_object($section)) {
				if(method_exists($section, 'get')) {
					$section	= $section->get();
				}

				if(method_exists($section, 'toArray')) {
					$section	= $section->toArray();
				}
			}
			return $section;
		}
		return null;
	}

	/**
	 * Gets all sections
	 *
	 * @param bool $convert
	 * @return array
	 */
	public function getSections($convert = false)
	{
		$sections	= isset($this->_sections) ? $this->_sections : array();
		$data		= array();
		foreach($sections as $name => $section) {
			$data[$name]	= $this->getSection($name, $convert);
		}
		return $data;
	}
}